---@class StoryHardManager
StoryHardManager = {}
local this = StoryHardManager

function StoryHardManager.init()
    this.powerGlobalData = decodeJsonStrToTabel(dataTable.getGlobalString(614));
    this.isEntryBattle = false;
end

---切换账号时会清理
function StoryHardManager.OnDestroy()
    this.isEntryBattle = false;
end

function StoryHardManager.getlastIdByCharpterID(id)
    if id==1 then
        return
    end
    local data =  G.dataTable["t_stagehardmode"];
    local datalist = {}
    for key, value in pairs(data) do
        if value.f_ChapterId==id-1 then
            table.insert(datalist, value.f_Id)
        end
    end
    table.sort(datalist,function(a,b) return a<b end)
    return datalist[1]
end

function StoryHardManager.isMaxID(id)
    if id >= logicMgr.StoryHardManager.getMaxIdByCharpterID(G.dataTable["t_global"]["622"].f_int_value) then
        return true
    else
        return false
    end
end



function StoryHardManager.getMaxIdByCharpterID(id)
    if id==1 then
        return
    end
    local data =  G.dataTable["t_stagehardmode"];
    local datalist = {}
    for key, value in pairs(data) do
        if value.f_ChapterId==id then
            table.insert(datalist, value.f_Id)
        end
    end
    table.sort(datalist,function(a,b) return a>b end)
    return datalist[1]
end

function StoryHardManager.getlastIdByCharpterID(id)
    local data =  G.dataTable["t_stagehardmode"];
    local datalist = {}
    for key, value in pairs(data) do
        if value.f_ChapterId==id+1 then
            table.insert(datalist, value.f_Id)
        end
    end
    table.sort(datalist,function(a,b) return a<b end)
    return datalist[1]
end

function StoryHardManager.getAllPower()
    return this.powerGlobalData[4] + logicMgr.EcoBuffManager.getEcoBuffByTypeAndItemId(Config.EcoBuffCondition.StoryPowerTimes)
end

function StoryHardManager.getBattlePower()
    return this.powerGlobalData[3];
end

function StoryHardManager.getRecoverPowerSpeed()
    return this.powerGlobalData[2];
end

function StoryHardManager.getDungeonTableDataById(id)
    id = id or this.getNextDungeonId();
    return G.dataTable["t_stagehardmode"][tostring(id)];
end

function StoryHardManager.getBattleMapName()
    if this.nextDungeonTableData then
        return this.nextDungeonTableData.f_AutoFightMap;
    end
end
-----------------------------------------------------------------------------------------
function StoryHardManager.setServerData(data)
    if data == nil then
        return ;
    end
    this.setPower(data.power);
    this.setDungeonId(data.maxPassDungeon);
    this.setPowerTime(tonumber(tostring(data.hardModeLastUpdateTime)) * 0.001);
    this.setDungeonPassTimes(data.dungeonPassTimes);
end

function StoryHardManager.setDungeonPassTimes(data)
    data = data or {}
    this.passTimesMap = {};
    for k, v in pairs(data) do
        this.passTimesMap[tostring(v.dungeonId)] = v.times;
    end
end
function StoryHardManager.updateDungeonPassTimes(id, num)
    this.passTimesMap = this.passTimesMap or {};
    this.passTimesMap[tostring(id)] = (this.passTimesMap[tostring(id)] or 0) + num;
end
function StoryHardManager.getDungeonPassTimes(id)
    this.passTimesMap = this.passTimesMap or {};
    return this.passTimesMap[tostring(id)] or 0;
end

function StoryHardManager.setPower(num)
    this.hardpower = num
end
function StoryHardManager.getPower()
    this.hardpower = this.hardpower or 0;
    this._time = this.getPowerTime();
    this._nowTime = HelperFunc.GetServerTime();
    this.maxPowrNum = this.getAllPower();
    if this.hardpower < this.maxPowrNum and this._nowTime >= this._time + this.getRecoverPowerSpeed() then
        this._recoverNum = math.floor((this._nowTime - this._time) / this.getRecoverPowerSpeed());
        this.hardpower = math.min(this.hardpower + this._recoverNum, this.maxPowrNum);
        this._time = this._time + this._recoverNum * this.getRecoverPowerSpeed();
        this.setPowerTime(this._time);
    end
    this._nextRecoverTime = -1;
    this._allRecoverTime = -1;
    if this.hardpower < this.maxPowrNum then
        this._nextRecoverTime = this._time + this.getRecoverPowerSpeed();
        this._allRecoverTime = this._time + (this.maxPowrNum - this.hardpower) * this.getRecoverPowerSpeed();
    end
    return this.hardpower, this._nextRecoverTime, this._allRecoverTime;
end

function StoryHardManager.setPowerTime(time)
    this.hardLastUpdateTime = time;
end
function StoryHardManager.getPowerTime()
    return this.hardLastUpdateTime or 0;
end
function StoryHardManager.IsEnergyFull()
    return this.getPower() >= this.getAllPower()
end

function StoryHardManager.setDungeonId(num)
    this.dungeonId = num
    this.curDungeonTableData = this.getDungeonTableDataById(this.dungeonId);
    if this.curDungeonTableData then
        this.setNextDungeonId(this.curDungeonTableData.f_NextStage)
        this.setChapterId(this.curDungeonTableData.f_ChapterId);
    elseif this.dungeonId == 0 then
        this.setNextDungeonId(101)
        this.setChapterId(1);
    end
end
function StoryHardManager.getDungeonId()
    return this.dungeonId or 0;
end

function StoryHardManager.setChapterId(id)
    this.chapterId = id
end
function StoryHardManager.getChapterId()
    return this.chapterId or 1;
end

function StoryHardManager.setChapterReward(data)
    this.chapterReward = data;
end
function StoryHardManager.getChapterReward()
    return this.chapterReward or {};
end

function StoryHardManager.setNextDungeonId(id)
    this.nextDungeonId = id
    this.nextDungeonTableData = this.getDungeonTableDataById(this.nextDungeonId);
    if this.nextDungeonTableData then
        this.setNextChapterId(this.nextDungeonTableData.f_ChapterId);
    else
        this.setNextChapterId(-1);
    end
end
function StoryHardManager.getNextDungeonId()
    if this.nextDungeonId == nil or this.dungeonId == 0 then
        this.nextDungeonId = 101;
    end
    return this.nextDungeonId;
end

function StoryHardManager.setNextChapterId(id)
    this.nextChapterId = id;
end
function StoryHardManager.getNextChapterId()
    return this.nextChapterId or 1;
end

function StoryHardManager.ShowStoryById()

end

function StoryHardManager.RecordStory()

end

function StoryHardManager.SetIndex()

end

---startStage 开始关卡(包含)
---endStage 结束关卡(不包含)
function StoryHardManager.HasStoryInRankLevel()

end

function StoryHardManager.MarkRecord()
    StoryHardManager.cache_record = {}
    if not StoryHardManager.cache_dungeon then
        StoryHardManager.cache_dungeon = {}
    end
end

--埋点记录
function StoryHardManager.CacheRecord(dungeonId, bossGroupId, win)
    if StoryHardManager.cache_record then
        -- local tb = {}
        -- tb.win = win
        -- tb.id = dungeonId
        -- tb.group_id = bossGroupId
        -- local config = G.dataTable["t_stagehardmode"][tostring(dungeonId)]
        -- if config then
        --     tb.have_story = config.f_IsStory
        -- end

        -- TAManager.TrackByDictionary("story_battle", tb)

        -- local record = StoryHardManager.cache_dungeon
        -- if record[dungeonId] then
        --     if win == 1 then
        --         record[dungeonId] = record[dungeonId] + 1

        --         if config and record[dungeonId] >= #config.f_MonsterTeamId then --
        --             StoryHardManager.UploadRecord(dungeonId, 0)
        --         end
        --     end
        -- else
        --     if win == 1 then
        --         record[dungeonId] = 1
        --     end
        -- end
        -- StoryHardManager.cache_record = nil
    end
end

--埋点上报
function StoryHardManager.UploadRecord(dungeonId, passNum)
    local tb = {}
    tb.id = dungeonId
    local config = G.dataTable["t_stagehardmode"][tostring(dungeonId)]
    if config then
        tb.have_story = config.f_IsStory
        tb.residue = config.f_FreqLimit - StoryHardManager.getDungeonPassTimes(dungeonId)
    end

    tb.sweep = passNum

    StoryHardManager.cache_record = tb

    TAManager.TrackByDictionary("story_battle_finish", tb)
end

function StoryHardManager.SetRanks(msg)
    this.otherData = msg.ranks
    if this.mydata == nil then
        this.mydata = {}
    end
    this.mydata.rank = msg.myRank
    this.mydata.score = msg.myScore
end

function StoryHardManager.GetRanks()
    return this.otherData
end

function StoryHardManager.GetMyRankInfo()
    return  this.mydata.rank, this.mydata.score
end

function StoryHardManager.autoBattle()
    local nowStage = logicMgr.MapManager.GetStoryHardAutoMapId();
    local cfg = logicMgr.StoryHardManager.getDungeonTableDataById(nowStage);
    if logicMgr.BattlePlaybackManager.GetGroupBossIsOver() and cfg.f_NextStage ~= -1 then
        nowStage = cfg.f_NextStage;
    end
    local nexeGroupId = logicMgr.BattlePlaybackManager.GetGroupBossJoinGroupId(EnumConst.FightTypeConst.StoryBattle);
    --local _towerDic = {};
    --_towerDic.stage_id = nowStage;
    --_towerDic.group_id = nexeGroupId;
    --TAManager.TrackByDictionary("tower_click_challenge", _towerDic);
    dungeonHandler.ReqHardGroupBossoHandler(nowStage, nexeGroupId, logicMgr.FormationManager.GetFormation(EnumConst.FormationType.StoryDuplicate))
end

function StoryHardManager.setIsEntryBattle(value)
    this.isEntryBattle = value or false;
end

function StoryHardManager.GetIsEntryBattle()
    return this.isEntryBattle;
end

return StoryHardManager